const fs = require('fs');

// 读取进货单数据文件
const data = fs.readFileSync('./通过插件拦截响应数据（淘宝进货单）.md', 'utf8');

// 提取JSON部分
const jsonStart = data.indexOf('{');
const jsonEnd = data.lastIndexOf('}') + 1;
const jsonData = data.substring(jsonStart, jsonEnd);

try {
  const pluginData = JSON.parse(jsonData);
  
  // 模拟extractTaobaoPurchaseOrders函数的逻辑
  const orders = [];
  const responseData = pluginData.response.data.data;
  
  // 遍历所有订单容器
  for (const key in responseData) {
    if (key.startsWith('shopOrderContainer_')) {
      const orderId = key.split('_')[1];
      
      // 获取订单信息
      const shopInfo = responseData[`shopInfo_${orderId}`];
      const orderPayment = responseData[`orderPayment_${orderId}`];
      
      if (!shopInfo || !orderPayment) {
        continue;
      }
      
      // 解析订单基本信息
      const orderDate = new Date(shopInfo.fields.createTime);
      const totalAmount = parseFloat(orderPayment.fields.actualFee.value.replace('￥', '')) || 0;
      const shopName = shopInfo.fields.shopName || ''; // 提取店铺名称
      
      console.log(`订单号: ${orderId}`);
      console.log(`店铺名称: "${shopName}"`);
      console.log(`下单时间: ${orderDate}`);
      console.log(`实付款: ${totalAmount}`);
      console.log('---');
    }
  }
} catch (error) {
  console.error('解析数据失败:', error);
}